﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace PickGold.Util.WordSpliter
{
	/// <summary>
	/// 同义词
	/// </summary>
	class Synonym
	{
		public string[][] Synonyms = null;//同义词组，文件中一行为一组，一组同义词以 “,” 分割

		public Dictionary<string, List<int>> SynonymGroup = null; //单词到同义词组的对应关系，一个单词可能对于多个同义词组

		public Synonym(string[][] synonyms)
		{
			this.Synonyms = synonyms;
			this.SynonymGroup = new Dictionary<string, List<int>>();
			if (synonyms == null)
				return;

			for (var i = 0; i < this.Synonyms.Length; i++)
			{
				var words = this.Synonyms[i];
				for (int ii = 0; ii < words.Length; ii++)
				{
					words[ii] = words[ii].Trim();

					List<int> idList;
					if (this.SynonymGroup.TryGetValue(words[ii], out idList))
					{
						if (idList[idList.Count - 1] == i)
							continue;

						idList.Add(i);
					}
					else
					{
						idList = new List<int>(1);
						idList.Add(i);
						this.SynonymGroup.Add(words[ii], idList);
					}
				}
			}
		}

		public List<string> GetSynonyms(string word)
		{
			word = word.Trim().ToLower();

			List<int> idList;
			if (!this.SynonymGroup.TryGetValue(word, out idList))
				return null;

			var result = new List<string>();

			foreach (int groupId in idList)
			{
				foreach (string w in Synonyms[groupId])
				{
					if (w == word)
					{
						continue;
					}

					if (result.Contains(w))
					{
						continue;
					}

					result.Add(w);
				}
			}

			return result;
		}
	}
}
